<?php
namespace app\admin\model;
use think\Model;


class Note extends Model
{

    protected static function init()
    {
        //添加之前的操作
        Pub::event('before_insert',function($note){
          if($_FILES['pic']['tmp_name']){
                $file = request()->file('pic');
                $info = $file->move(ROOT_PATH . 'public' . DS . 'static/uploads');
                if($info){
                    $note['pic']='/LOLforum/'.'public/static/'.'uploads/'.date('Ymd',time()).'/'.$info->getFileName();
                }
            }
      });


        Pub::event('before_update',function($note){
          if($_FILES['pic']['tmp_name']){
                $notes=Pub::find($note->id);
                $thumbpath=$_SERVER['DOCUMENT_ROOT'].$notes['pic'];
                if(file_exists($thumbpath)){
                    @unpub($thumbpath);
                }
                $file = request()->file('pic');
               $info = $file->move(ROOT_PATH . 'public' . DS . 'static/uploads');
                if($info){
                    $note['pic']='/LOLforum/'.'public/static/'.'uploads/'.date('Ymd',time()).'/'.$info->getFileName();
                    db('note')->where('id',$note->data['id'])->update(['pic' => $note['pic']]);
                }

            }
      });

        Pub::event('before_delete',function($note){         
                $notes=Pub::find($note->id);
                $thumbpath=$_SERVER['DOCUMENT_ROOT'].$notes['pic'];
                if(file_exists($thumbpath)){
                    @unpub($thumbpath);
                }
        });

    }


	public function cate(){
        
		return $this->belongsTo('cate','cateid');
	}


}
